package cn.com.ddapp.ddm.ui;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.com.ddapp.ddm.AppConfig;
import cn.com.ddapp.ddm.MainActivity;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.base.BaseActivity;
import cn.com.ddapp.ddm.base.BaseApplication;
import cn.com.ddapp.ddm.bean.CityBean;
import cn.com.ddapp.ddm.customview.MySearchFlowLayout;
import cn.com.ddapp.ddm.inter.OkUtilCallback;
import cn.com.ddapp.ddm.ui.empty.EmptyLayout;
import cn.com.ddapp.ddm.util.NetUtils;
import cn.com.ddapp.ddm.util.TLog;
import cn.com.ddapp.ddm.util.Utility;
import okhttp3.Call;

/**
 * Created by Administrator on 2016/8/26 0026.//城市列表
 */
public class CitysListActivity extends BaseActivity {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.titletext)
    TextView titletext;
    @BindView(R.id.cityList)
    ListView cityList;
    @BindView(R.id.error_layout)
    EmptyLayout mEmptyLayout;
    @BindView(R.id.img_error_layout)
    ImageView img;
    private CityBean cityBean;
    private HashMap<String, Integer> attrsMap;
    private TextView textView;
    private List<Map<Integer,String >> list=new ArrayList<>();
    private List<CityBean.AreaListBean> addrGroup;

    @Override
    public void initView() {
        setTitle("选择地区");
        setTitleLeftImg(R.mipmap.back_icon, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initData();
            }
        });
    }

    @Override
    public void initData() {
        initCityData();//地区下载
    }

    private void initCityData() {
        if (!NetUtils.isConnected(this)) {
            Toast.makeText(CitysListActivity.this, "请检查你的网络", Toast.LENGTH_SHORT).show();
        } else {
            mEmptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
            OkUtilCallback okUtilCallback = new OkUtilCallback(mEmptyLayout) {
                @Override
                public void onMyResponse(String response, int id) {
//                    TLog.log(getFragmentTag(), response);
//                    Log.e("TAG", "onMyResponsesponse: " + response);
                    cityBean = gson.fromJson(response, CityBean.class);
                    if ((!AppConfig.RESULT_OK.equalsIgnoreCase(cityBean.getResult())) && cityBean.getLogin()) {
                        mEmptyLayout.setErrorType(EmptyLayout.NO_LOGIN);
                    } else if ((!AppConfig.RESULT_OK.equalsIgnoreCase(cityBean.getResult()))) {
                        BaseApplication.getIntstance().showToast(cityBean.getResult(), Toast.LENGTH_SHORT, 0, Gravity.TOP);
                    } else if ((AppConfig.RESULT_OK.equalsIgnoreCase(cityBean.getResult()))) {
                        cityList();//内容视频
                    }
                }

                @Override
                public void onError(Call call, Exception e, int id) {
                    super.onError(call, e, id);
                    mEmptyLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
                }
            };
            OKUtilAPI.get(OKUtilAPI.CITYLIST_URL, "", okUtilCallback);
        }
    }

    private void cityList() {
         addrGroup = cityBean.getAreaList();
         cityList.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return addrGroup.size();
            }

            @Override
            public Object getItem(int position) {
                return addrGroup.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(final int position, View convertView, ViewGroup parent) {
                ViewHolder holder = null;
                convertView = LayoutInflater.from(CitysListActivity.this).inflate(R.layout.item_city_list, parent, false);
                holder = new ViewHolder(convertView);

                MySearchFlowLayout.MarginLayoutParams params = new MySearchFlowLayout.MarginLayoutParams(
                        MySearchFlowLayout.LayoutParams.WRAP_CONTENT,
                        MySearchFlowLayout.LayoutParams.WRAP_CONTENT
                );
                params.leftMargin = 10;
                params.topMargin = 10;

                CityBean.AreaListBean.AreaBean area = addrGroup.get(position).getArea();
                holder.cityTop.setText(area.getName());//设置省份
                final List<CityBean.AreaListBean.ListBean> list = addrGroup.get(position).getList();
                for (int i = 0; i < list.size(); i++) {
                    final CityBean.AreaListBean.ListBean listBean = list.get(i);
                    TextView textView=new TextView(CitysListActivity.this);
                    textView.setText(listBean.getName());
                    textView.setLayoutParams(params);
                    textView.setPadding(10, 10, 20, 10);
                    holder.mFlowlayout.addView(textView);

                    textView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            int id = listBean.getId();
                            Intent intent=new Intent(CitysListActivity.this,CityMoreListActivity.class);
                            intent.putExtra("id",String.valueOf(id));
                            startActivity(intent);
                        }
                    });
                }
                return convertView;
            }
        });
    }

    class ViewHolder {
        @BindView(R.id.city_Top)
        TextView cityTop;
        @BindView(R.id.flowlayout)
        MySearchFlowLayout mFlowlayout;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_city_list;
    }

    @Override
    protected String getFragmentTag() {
        return null;
    }
}
